package cn.jwcat.controller;

import cn.jwcat.entity.Notice;
import cn.jwcat.service.INoticeService;
import cn.jwcat.utils.otherUtils;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告操作
 */
@Transactional
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    INoticeService noticeService;

    @GetMapping
    @ApiOperation("获取公告全部列表")
    public Result selectList() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getCreateTime);
        List<Notice> notices = noticeService.list(queryWrapper);
        return Result.success(notices);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取公告内容")
    public Result selectByPrimaryKey(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    @GetMapping("/{page}/{size}")
    @ApiOperation("公告的分页查询")
    public Result bookPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getCreateTime);
        PageHelper.startPage(page, size);
        List<Notice> notices = noticeService.list(queryWrapper);
        PageInfo<Notice> noticePageInfo = new PageInfo<>(notices);
        return Result.success(noticePageInfo);
    }

    @PostMapping("/{page}/{size}")
    @ApiOperation("条件分页查询公告")
    public Result inquiryNotice(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody Notice notice) {
        if ("".equals(notice.getTitle()) && "".equals(notice.getContext()) && notice.getTime().length == 0) {
            return Result.error();
        }
        PageHelper.startPage(page, size);
        List<Notice> notices = noticeService.inquiry(notice);
        PageInfo<Notice> noticePageInfo = new PageInfo<>(notices);
        return Result.success(noticePageInfo);
    }

    @PostMapping
    @ApiOperation("添加公告内容")
    public Result newNotice(@RequestBody Notice notice) {
        if ("".equals(notice.getTitle()) && "".equals(notice.getContext())) {
            return Result.error("数据操作失败，请检查输入内容");
        }
        boolean bool = noticeService.save(notice);
        return otherUtils.dealBool(bool);
    }

    @PutMapping
    @ApiOperation("修改公告内容")
    public Result putNotice(@RequestBody Notice notice) {
        if ("".equals(notice.getTitle()) || "".equals(notice.getContext())) {
            return Result.error("数据操作失败，请检查输入内容");
        }
        boolean bool = noticeService.updateById(notice);
        return otherUtils.dealBool(bool);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除公告")
    public Result noticeDelete(@PathVariable("id") Integer id) {
        boolean bool = noticeService.removeById(id);
        return otherUtils.dealBool(bool);
    }
}
